/*-----------------------------------------------------------------------\
|             Millennium                                                 |
\------------------------------------------------------------------------/
|  Header   | cmm - comms management module                              |
\------------------------------------------------------------------------\
|  By       | Hamdan & Bilko                                             |
\------------------------------------------------------------------------/
|  Date     | September 2004                                             |
\------------------------------------------------------------------------\
|  Platform | Unicapt32                                                  |
\------------------------------------------------------------------------\
|  Version  | 1.0                                                        |
\-----------------------------------------------------------------------*/
#ifndef __CMM_H__
#define __CMM_H__

//---------------------------------------------
// Comms Device Types
//---------------------------------------------


typedef enum
{
  cmmDeviceNAC,     // 0
  cmmDeviceRS232,     // 1
  cmmDeviceExternal,  // 2
  cmmDeviceEthClient, // 3
  cmmDeviceEthServer,  // 4
  cmmDeviceModem,  //assy
  cmmDevicePPP,
  cmmDeviceUSB,
  cmmDeviceGPRS,
#ifdef _SSL_ENABLE_
  cmmDeviceSSL
#endif
} cmmDevice_t;

//---------------------------------------------
// Connection Status Types
//---------------------------------------------
typedef enum
{
  cmmConnectedNone,
  cmmConnectingPrimary,
  cmmConnectingSecondary,
  cmmConnectedPrimary,
  cmmConnectedSecondary,
  cmmConnectedPeer,
  cmmConnectedGPRS
} cmmConnectionStatus_t;

//---------------------------------------------
// Ethernet Configuration Types
//---------------------------------------------
typedef enum
{
  cmmEthLocal,
  cmmEthPeer,
  cmmEthHostPrimary,
  cmmEthHostSecondary
} cmmEthConfig_t;

//---------------------------------------------
// Comms Protocol types
//---------------------------------------------
typedef enum
{
  cmmProtocolHDLC,        // 0 = HDLC
  cmmProtocolTransparent, // 1 = No Protocol
  cmmProtocolAsyncEnq,    // 2 = ENQ STX LEN MSG ETX LRC
  cmmProtocolAsyncLength, // 3 = STX LEN MSG ETX LRC 
  cmmProtocolAsyncDle,    // 4 = STX DLE/MSG ETX LRC 
  cmmProtocolAsyncNone,   // 5 = STX MSG(ASCII) ETX LRC 
  cmmProtocolAsyncArgent, // 6 = Argent Terminal Adapter
  cmmProtocolTCP,         // 7 = TCP/IP
  cmmProtocolUDP,         // 8 = UDP
  cmmProtocolAsyncLenCRC, // 9 = STX LEN MSG ETX CRC
  cmmProtocolColesMyer    // 10  = STX LEN MSG ETX CRCprop 
  
 
} cmmProtocol_t;

//---------------------------------------------
// External Device Types
//---------------------------------------------
typedef enum
{
  cmmExtSerial,
  cmmExtEthernet
} cmmExternalType_t;

//---------------------------------------------
// Length Format
//---------------------------------------------
typedef enum
{
  cmmLengthBCD,     	// 0	100 = 0x01 0x00
  cmmLengthHexHiLo,     // 1	100 = 0x00 0x64
  cmmLengthHexLoHi,  	// 2	100 = 0x64 0x00
  cmmLengthNone 		// 3	NULL
} cmmLengthFormat_t;


//---------------------------------------------
// Return codes (int16)
//---------------------------------------------
typedef enum
{
  cmmSuccess,					//0
  cmmErrTimeout,				//  1
  cmmErrLostCarrier,				//  2
  cmmErrGeneral,				//  3
  cmmErrInitFailed,    			//  4
  cmmErrRxBufferOverflow, 		// 5
  cmmErrAsyncNoAck,   			// 6
  cmmErrAsyncNoResponse,   		// 7
  cmmErrBadLRC,      				//8
  cmmErrNoETX,     				//9
  cmmErrNoENQ,					//10
  cmmErrInvalidLength,           // 11
  cmmErrNotInitialised,             		//12
  cmmErrInitialised,				// 13
  cmmErrTxBufferOverflow,		 // 14
  cmmErrApplicationMissing,		//15
  cmmErrInternalBufferOverflow,    // 16
  cmmErrPhoneNumberMissing, 	//   17
  cmmErrUserCancel,                    // 18 
  cmmErrUserTimeout,                 // 19 
  cmmErrOffBase,				//20
  cmmErrDeviceInUse,			// 21
  cmmErrSystem,  				 // 22
  cmmErrDialNoCarrier,			//23
  cmmErrDialNoTone,				//24
  cmmErrDialFailure,			//25
  cmmErrDialBusy,				//26
  cmmErrEthInterfaceDown,  //27
  cmmErrEthNoRemoteIpAddr,  //28
  cmmErrEthHostConnectFail,   //29
  cmmErrEthHostDisconnected,	//30
  cmmErrSignalCtsOff,
  cmmErrNoEOT
} cmmResult_t;
  
//--------------------------
// Comms Commands (uint32)
//--------------------------
typedef enum
{
  CMM_COMMAND_INIT,
  CMM_COMMAND_SEND,
  CMM_COMMAND_RECV,
  CMM_COMMAND_LISTEN,
  CMM_COMMAND_STATUS,
  CMM_COMMAND_END,
  CMM_COMMAND_LAST_ERROR_GET,	// 6. Error/Error Text
  CMM_COMMAND_MODEM_INFO_GET,	// 7. Firmware Version
  CMM_COMMAND_DEBUG_SET,		// 8
  CMM_COMMAND_DEBUG_GET,		// 9
  CMM_COMMAND_DIAL_INFO_SET,	// 10 Blind/Busy Tone/Pulse/PABX/Connection Mode
  CMM_COMMAND_DIAL_INFO_GET,
  CMM_COMMAND_ETH_INFO_SET,   // Address/Mask/Gateway/DHCP
  CMM_COMMAND_ETH_INFO_GET,   //                       + /Presence
  CMM_COMMAND_EDH_INFO_SET,   // Heartbeat/Params/AutoReceive
  CMM_COMMAND_EDH_INFO_GET,		//15
  CMM_COMMAND_RS232_INFO_SET, // Async Ack Wait
  CMM_COMMAND_RS232_INFO_GET,
  CMM_COMMAND_DEVICE_FALLBACK_SET,
  CMM_COMMAND_CONFIGURATION_PRINT,
  CMM_COMMAND_LNET_UP,      // UP LNET
  CMM_COMMAND_LNET_DOWN,    // DOWN LNET
  CMM_COMMAND_PING,
  CMM_COMMAND_DEVICE_INFO_SET,
  CMM_COMMAND_DEVICE_INFO_GET,
  CMM_COMMAND_FILE_SEND  
} cmmCommand_t;



//aan
#define FLUSH_SEND_BUFFER	0x01
#define FLUSH_RECV_BUFFER	0x02
//aan

typedef struct 
{
	uint16		comport;////1-COM1 2-COM2 3-COM3 4-blablabla	
	bits32		dataSize:4;		
	bits32  		parity:4;	
	bits32		stopBits:4;
	bits32		rfu:12;
	uint32 		speed;
	
}cmmRS232InitParam_t;



/*************************************/

typedef struct 
{		
	uint32	speed;
	bits32 	fastconnect:1;
	bits32 	blindmode:1;
	bits32 	pulsedial:1;
		
	bits32 	debug:1;
	bits32 	usercancel:1;
	bits32 	foreground:1;
	bits32	ASCIImode:1;
	bits32 	backup:1;
	

	
	char		pri_num[17];			// Primary Host Number
	char		sec_num[17];			// Secondary Host Number

	
}cmmDialParam_t;


/*************************************/

typedef struct 
{	
	uint8	username[32];
	uint8	password[32];
	bits32	lenFormat:4;
	bits32	rfu:28;
	uint8	ipAddrPri[4];
	int16	portNumPri;
	//uint16	portNumPri;
	uint8	ipAddrSec[4];
	int16	portNumSec;
	//uint16	portNumSec;
	
	
}cmmTcpipInitParam_t;


typedef struct
{
	cmmDialParam_t			Dial;
	cmmRS232InitParam_t		RS232;
	cmmTcpipInitParam_t		IP;		
}Phyical_link;

 typedef struct
{
	uint16 	cmmmode:8;
	uint16 	protocol:8;
	uint16	pri_attempts:8;
	uint16	sec_attempts:8;	
	
	uint16	rx_timeout;	
	uint16	connect_timeout;	 
	uint16	inter_char_time_out;
	Phyical_link cmm;	
}INIT_COMMS_PARAMS;


 
//---------------------------------------------
// Exported Functions
//---------------------------------------------
//cmmResult_t cmmInit(uint32* deviceHandle,INIT_COMMS_PARAMS *icp);
cmmResult_t cmmInit(uint32* deviceHandle,INIT_COMMS_PARAMS *icp, bits32  predial);
cmmResult_t cmmEnd(uint32 deviceHandle);
int16 cmmSend(uint32 deviceHandle, uint8 const* msg, size_t length);
int16 cmmRecv(uint32 deviceHandle, uint8* msg, uint16* length, size_t maxSize);
int16 cmmListen(uint32 deviceHandle);
int16 cmmStatus(uint32 deviceHandle);
int16 cmmGeneral(void);
int16 cmmDebug(char const* text, void const* buffer, size_t bufferLength);
int16 cmmPrintf(char const* format, ...);
int16 cmmTestMsg(const S_TRANSIN *msg);
void InitDefaultConfig(void);
void InitClearHandle(void);

//aan
void cmmFlushComBuffer(uint32 deviceHandle, uint8 buff2Clear);
cmmResult_t cmmGPRS_Stop(void);
cmmResult_t cmmGPRS_Start(uint8 gprsPredial,uint8 check);
cmmResult_t cmmGPRSInit(uint8 gprsPredial,uint8 check);

//04012011
int cmmGetGPRSNetworkStatus(int *nStatus);
//aan

//#ifdef __TEST_SEC_DIAL__
//cmmResult_t cmmLastErr(uint32 deviceHandle);
cmmResult_t cmmLastErr(int connRetval);
void cmmSetDial(uint8 DialPri);
uint8 GetcmmSetDial(void);
void killthread(void);
//#endif


//aan, 15 Juli 2007
cmmResult_t cmmGetConnectionStatus(void);

cmmResult_t cmmGetConnectionStatus2(uint32 deviceHandle);

#ifdef _SSL_ENABLE_

cmmResult_t  cmmSslEnd();
int16 cmmSslSend(const uint8 * msg, size_t length);
int16 cmmSslRecv(uint8* msg, uint16* length, size_t maxSize);

#endif

//// Types //////////////////////////////////////////////////////
typedef struct
{
	int m_nLink;
	int m_nBaudrate;
	int m_nBitsPerByte;
	int m_nStopBits;
	int m_nParity;
	int m_nFlowCtrl;

	int m_bUseModem;
	int m_nModemCmdTerminator;
	char m_szInitString[LL_MODEM_L_INIT_STRING_MAX + 1];
	char m_szPhoneNumber[LL_MODEM_L_PHONE_NUMBER_MAX + 1];


	int m_bUseGsm;
	int m_nGsmMode;
	int m_nGsmModulation;
	int m_nConnectionTimeout;

	char m_szPinCode[LL_GPRS_L_PIN_CODE_MAX+1];

	int m_bUseHdlc;

	int m_bUsePpp;
	char m_szLogin[LL_PPP_L_LOGIN_MAX + 1];
	char m_szPassword[LL_PPP_L_PASSWORD_MAX + 1];

	int m_bUseTcpIp;
	char m_szHostName[LL_TCPIP_L_HOST_NAME_MAX + 1];
	int m_nPort;

	int m_bUseGprs;
	char m_szAPN[LL_GPRS_L_APN_MAX + 1];

	int m_nTestMode;
} T__LL_CONFIGURATION;

#endif


